define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: () => {
            new Vue({
                el: "#bannerIndex",
                data() {
                    return {
                        position_map: [],
                        allAjax: true,
                        tableAjax: false,
                        bannerData: [],
                        currentPage: 1,
                        limit: 10,
                        offset: 0,
                        totalPage: 0,
                        where: {
                            position: ''
                        }
                    }
                },
                created() {
                    this.position_map = Config.position_map;
                },
                mounted() {
                    this.where.position = String(this.position_map[0].name);
                },
                watch: {
                    'where.position': {
                        handler(newV) {
                            this.limit = 10;
                            this.offset = 0;
                            this.getData();
                        }
                    }
                },
                methods: {
                    // 获取列表数据
                    getData() {
                        let that = this;
                        if (!that.allAjax) {
                            that.tableAjax = true;
                        }
                        Fast.api.ajax({
                            url: 'health/website/banner/index',
                            loading: false,
                            type: 'GET',
                            data: {
                                position: that.where.position,
                                limit: that.limit,
                                offset: that.offset
                            }
                        }, function (ret, res) {
                            that.bannerData = ret.rows;
                            that.totalPage = ret.total;
                            that.allAjax = false;
                            that.tableAjax = false;
                            return false;
                        }, function (ret, res) {
                            that.allAjax = false;
                            that.tableAjax = false;
                        })
                    },
                    pageSizeChange(val) {
                        this.offset = 0;
                        this.currentPage = 1
                        this.limit = val;
                        this.getData();
                    },
                    pageCurrentChange(val) {
                        this.offset = (val - 1) * this.limit;
                        this.currentPage = val;
                        this.getData();
                    },
                    //操作按钮
                    operation(opttype, id, status) {
                        let that = this;
                        switch (opttype) {
                            case 'del':
                                that.$confirm('此操作将删除轮播图, 是否继续?', '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    type: 'warning'
                                }).then(() => {
                                    Fast.api.ajax({
                                        url: 'health/website/banner/del/ids/' + id,
                                        loading: false,
                                        type: 'POST',
                                    }, function (ret, res) {
                                        that.getData()
                                    })
                                }).catch(() => {
                                });

                                break;
                            case 'status':
                                let str = status == 1 ? 0 : 1;
                                that.$confirm('此操作将修改轮播图状态, 是否继续?', '提示', {
                                    confirmButtonText: '确定',
                                    cancelButtonText: '取消',
                                    type: 'warning'
                                }).then(() => {
                                    Fast.api.ajax({
                                        url: `health/website/banner/setStatus/ids/${id}/status/${str}`,
                                        loading: false,
                                    }, function (ret, res) {
                                        that.getData();
                                    })
                                }).catch(() => {
                                });
                                break;
                            case 'create':
                                Fast.api.open("health/website/banner/add?position=" + that.where.position, "创建轮播图", {
                                    callback(data) {
                                        if (data.data) {
                                            that.getData();
                                        }
                                    }
                                });
                                break;
                            case 'edit':
                                Fast.api.open("health/website/banner/edit?ids=" + id, "编辑轮播图", {
                                    callback(data) {
                                        if (data.data) {
                                            that.getData();
                                        }
                                    }
                                });
                                break;
                        }
                    },
                }
            })
        },
        add: function () {
            Controller.detailInit('add');
        },
        edit: function () {
            Controller.detailInit('edit');
        },
        detailInit: function (type) {
            function urlParmas(par) {
                let value = ""
                window.location.search.replace("?", '').split("&").forEach(i => {
                    if (i.split('=')[0] == par) {
                        value = JSON.parse(decodeURI(i.split('=')[1]))
                    }
                })
                return value
            }
            var detailInit = new Vue({
                el: "#detailInit",
                data() {
                    return {
                        optType: type,
                        id: urlParmas('ids'),
                        detailForm: {},
                        detailFormInit: {
                            name: '',
                            image: '',
                            status: 1,
                            type: 0,
                            params: { appid: '', page: '' },
                            position: 1,
                        },
                        typeMap: [],
                        rules: {
                            name: [{
                                required: true,
                                message: '请输入轮播图名称',
                                trigger: 'blur'
                            }],
                            image: [{
                                required: true,
                                message: '请上传轮播图片',
                                trigger: 'blur'
                            }],
                            type: [{
                                required: true,
                                message: '请选择跳转方式',
                                trigger: 'blur'
                            }],
                        }

                    }
                },
                created() {
                    this.typeMap = Config.type_map
                    this.detailForm = JSON.parse(JSON.stringify(this.detailFormInit));
                    if (this.optType == 'edit') {
                        for (key in this.detailForm) {
                            this.detailForm[key] = Config.row[key]
                        }
                    } else {
                        if (Config.position) {
                            this.detailForm.position = Config.position;
                        }
                    }
                },
                methods: {
                    addImg(type, index, multiple) {
                        let that = this;
                        parent.Fast.api.open("general/attachment/select?multiple=" + multiple, "选择图片", {
                            callback: function (data) {
                                that.detailForm.image = data.url;
                            }
                        });
                        return false;
                    },
                    delImg(type, index) {
                        this.detailForm.image = ''
                    },
                    submitForm(type, formName) {
                        let that = this;
                        if (type) {
                            this.$refs[formName].validate((valid) => {
                                if (valid) {
                                    let formData = JSON.stringify(that.detailForm);
                                    if (that.optType == 'edit') {
                                        Fast.api.ajax({
                                            url: 'health/website/banner/edit/ids/' + that.id,
                                            loading: true,
                                            data: {
                                                data: formData
                                            }
                                        }, function (ret, res) {
                                            Fast.api.close({ data: true });
                                        })
                                    } else {
                                        Fast.api.ajax({
                                            url: 'health/website/banner/add',
                                            loading: true,
                                            data: {
                                                data: formData
                                            }
                                        }, function (ret, res) {
                                            Fast.api.close({ data: true });
                                        })
                                    }
                                } else {
                                    return false;
                                }
                            });
                        } else {
                            Fast.api.close({ data: false });
                        }
                    },
                },
            })
        },
    }
    return Controller;
});